package service.impl;

import dao.AdminDao;
import dao.impl.AdminDaoImpl;
import domain.*;
import service.AdminService;

import java.util.List;
import java.util.Map;

/**
 * @author: 爱学Java 不想秃头的 KK
 * @date: 2023/7/6 10:57
 * @version: 1.0
 * @Created with IntelliJ IDEA.
 * @Description:
 */
public class AdminServiceImpl implements AdminService {
    AdminDao adminDao = new AdminDaoImpl();


    @Override
    public Administrator login(Administrator admin) {
        return adminDao.findUsersByUsernameAndPassword(admin.getUsername(), admin.getPassword());
    }


    @Override
    public PageBean findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //1.创建一个空对象
        PageBean pb = new PageBean(); //后面也加上了泛型是为了向下兼容

        //2.调用dao查询总记录数
        int totalCount = adminDao.findTotalCount(condition);

        //3.计算总页码数
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        //4.安全检验,如果当前页小于1,跳转到最后一页,如果当前页大于总页数跳转到第一页
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        //5.计算开始查询的索引
        int start = (currentPage - 1) * rows;
        //6.调用dao查询list集合
        List<User> list = adminDao.findByPage(start, rows, condition);

        //7.设置属性,为什么放在最后?
        //因为如果提前设置会导致PageBean中的的当前页会导致第四部要防止的问题失效
        //这个是因为PageBean的属性已经提前设置好了,而不是更新之后的
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        //8.返回PageBean对象
        return pb;
    }

    @Override
    public void deleteUser(String uid) {
        adminDao.delete(Integer.parseInt(uid));
    }


    @Override
    public void delSelectedUsersById(String[] ids) {
        if (ids != null && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                adminDao.delete(Integer.parseInt(ids[i]));
            }
        }
    }

    @Override
    public PageBean findArticleByPage(String currentPage_, String rows_) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        PageBean pb = new PageBean();
        int totalCount = adminDao.findTotalCountFromArticle();
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Article> list = adminDao.findByPageFromArticle(start, rows);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        return pb;
    }

    @Override
    public User findUserByCode(String code) {
        return adminDao.findUserByCode(code);
    }

    @Override
    public Category findCategory(int cid) {
        return adminDao.findCategory(cid);
    }

    @Override
    public int updateUsernameAndPassword(int id, String username, String password) {
        int row = adminDao.updateUsernameAndPassword(id,username,password);
        return row;
    }

    @Override
    public void deleteCategory(String cid) {
        adminDao.deleteCategoryByCid(Integer.parseInt(cid));
    }

    @Override
    public PageBean findCategoryByPage(String currentPage_, String rows_, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        PageBean pb = new PageBean();
        int totalCount = adminDao.findTotalCountFromCategory();
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Category> list = adminDao.findByPageFromCategory(start, rows);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        return pb;
    }

    @Override
    public void addCategory(String cname) {
        adminDao.addCategory(cname);
    }

    @Override
    public PageBean findLabelByPage(String currentPage_, String rows_, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(currentPage_);
        int rows = Integer.parseInt(rows_);
        PageBean pb = new PageBean();
        int totalCount = adminDao.findTotalCountFromLabel();
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows) + 1;
        if (currentPage < 1) {
            currentPage = totalPage; //防止前台用户点击查询当前页的左页导致当前页不存在致使服务器500
        }
        if (currentPage > totalPage) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * rows;
        List<Label> list = adminDao.findByPageFromLabel(start, rows);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(rows);
        pb.setTotalCount(totalCount);
        pb.setTotalPage(totalPage);
        pb.setList(list);
        return pb;
    }

    @Override
    public void deleteLabel(String lid) {
        adminDao.deleteLabel(Integer.parseInt(lid));
    }

    @Override
    public void addLabel(String lname) {
        adminDao.addLabel(lname);
    }

    @Override
    public Administrator findOneAdmin(String username) {
        return adminDao.findOneAdmin(username);
    }
}
